package com.tian.photopicker.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.DrawableImageViewTarget;
import com.bumptech.glide.request.transition.Transition;
import com.tian.photopicker.bean.Photo;
import com.tian.photopicker.photoview.PhotoView;

import java.util.ArrayList;
import java.util.List;

/**
 * 预览adapter
 */

public class PhotoPagerAdapter extends PagerAdapter {
    private Context context;
    private List<String> photos = new ArrayList<>();
    private RequestManager requestManager;
    public PhotoPagerAdapter(Context context, List<String> photos) {
        this.context = context;
        this.photos = photos;
        this.requestManager = Glide.with(context);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        PhotoView photoView = new PhotoView(context);
        photoView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        photoView.enable();
        photoView.setMaxScale(3f);
        photoView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        RequestOptions options = new RequestOptions();
        options.dontAnimate()
                .dontTransform();
        requestManager.setDefaultRequestOptions(options)
                .load(photos.get(position)).into(new DrawableImageViewTarget(photoView){
            @Override
            public void onStart() {
                super.onStart();
            }
            @Override
            public void onLoadStarted(@Nullable Drawable placeholder) {
                super.onLoadStarted(placeholder);
            }

            @Override
            public void onResourceReady(Drawable resource, @Nullable Transition<? super Drawable> transition) {
                super.onResourceReady(resource, transition);
            }

            @Override
            public void onStop() {
                super.onStop();
            }

            @Override
            public void onLoadCleared(@Nullable Drawable placeholder) {
                super.onLoadCleared(placeholder);
            }

            @Override
            public void onDestroy() {
                super.onDestroy();
            }

            @Override
            public void onLoadFailed(@Nullable Drawable errorDrawable) {
                super.onLoadFailed(errorDrawable);
            }

        });
        container.addView(photoView);
        return photoView;
    }

    @Override
    public int getCount() {
        return photos.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
        requestManager.clear((View) object);
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }
}
